package com.example.leero.graduation.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by Leero on 2017-4-26.
 */

public class EditPasswordActivity extends AppCompatActivity {

    private EditText et_old_pwd, et_new_pwd, et_repetition_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);

        findViewById(R.id.btn_forget_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditPasswordActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
            }
        });

        // 标题栏
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("修改密码");
        TextView submit = (TextView) findViewById(R.id.tv_run_setting);
        submit.setText("提交");
        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInput()){
                    updatePassword();
                }
            }
        });
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
        et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
        et_repetition_pwd = (EditText) findViewById(R.id.et_repetition_pwd);
    }

    private Boolean checkInput() {
        String oldPwd = et_old_pwd.getText().toString();
        String newPwd = et_new_pwd.getText().toString();
        String repetitionPwd = et_repetition_pwd.getText().toString();

        if (StringUtil.isEmpty(oldPwd) || StringUtil.isEmpty(newPwd) || StringUtil.isEmpty(repetitionPwd)) {
            Toast.makeText(this, "请输入完成", Toast.LENGTH_SHORT).show();
            return false;
        } else if (oldPwd.length() < 8 || newPwd.length() < 8) {
            Toast.makeText(this, "密码长度至少为8位", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!StringUtil.equals(newPwd, repetitionPwd)) {
            Toast.makeText(this, "两次密码输入不正确", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private void updatePassword() {
        OkHttpUtils.post().url(API.RESET_PASSWORD)
                .addParams("user_id", App.getLoginUser().id+"")
                .addParams("old_password", et_old_pwd.getText().toString())
                .addParams("new_password", et_new_pwd.getText().toString())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getBoolean("state")) {
                        new AlertDialog.Builder(EditPasswordActivity.this)
                                .setMessage("密码修改成功！")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        App.logout();
                                        startActivity(new Intent(EditPasswordActivity.this, LoginActivity.class));
                                    }
                                }).show();
                    } else {
                        new AlertDialog.Builder(EditPasswordActivity.this)
                                .setMessage(jsonObject.getString("message"))
                                .setPositiveButton("确定", null).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
